$(document).ready(function(){
	var cgv = 0;
	var sleigh = $("#sleigh-wrap");
	var airplane = $("#airplane");
	
	// cgving functions
	$(window).scroll(function() {
		cgv = $(this).scrollTop();
		// always on top menu
		if (cgv >69) {
			$("#nav-wrap").css({"position":"fixed","top":"0px"}).addClass("scrolled");
		}else{
			$("#nav-wrap").css({"position":"absolute","top":"69px"}).removeClass("scrolled");
		}
		// sleigh
		if(cgv < 350) {
			$("#snow-tail").fadeIn("fast");
			sleigh.stop().animate({"left": $(".header").offset().left + cgv*(960+$(".header").offset().left+sleigh.width())/350 + "px","top": cgv + 450 + "px"},{duration: cgv*10,easing: "easeInOutCubic",complete:function(){
					$("#snow-tail").fadeOut("fast");
			}});
		}
		// airplane
		if(cgv > 250){
			airplane.stop().animate({"right": (cgv-250)*($(document).width()+258)/200 + "px","top": cgv + 475 + "px"},{duration: cgv*10,easing: "easeInOutSine"});
		}else{
			airplane.stop().animate({"right": - 258 + "px","top": 475 + "px"},{duration: 1000,easing: "easeInOutSine"});
		}
		if(cgv > 200) $("#section-2 .content").stop().animate({"opacity": (cgv-200)/200},{duration: "slow",easing: "easeInOutCubic"});
		if(cgv > 570 && cgv < 1160){
			$("#bg-house").stop().animate({"right": $(".header").offset().left + "px"},{duration: 500,easing: "easeOutBack"});
			$("#bg-market").stop().animate({"left": $(".header").offset().left + 365 + "px"},{duration: 750,easing: "easeOutBack",complete:function(){
				$("#people-2").stop().animate({"right": $(".header").offset().left + 20 + "px"},{duration: 1000,easing: "easeOutBack"});
				$("#bg-case").stop().animate({"right": $(".header").offset().left + 48 + "px"},{duration: 1000,easing: "easeOutBack"});
			}});
			$("#bg-church").stop().animate({"left": $(".header").offset().left + 539 + "px"},{duration: 1000,easing: "easeOutBack"});
			$("#bg-dinh").stop().animate({"right": $(".header").offset().left + 29 + "px"},{duration: 1000,easing: "easeOutBack"});
			$("#bg-sec-2-tree").stop().animate({"left": $(".header").offset().left + 250 + "px"},{duration: 1500,easing: "easeOutBack"});
			$("#bg-sec-2-people").stop().animate({"right": $(".header").offset().left + 219 + "px"},{duration: 1750,easing: "easeOutBack"});
		}else{
			$("#bg-case").stop().animate({"right": -172 + "px"},{duration: 500,easing: "easeInBack"});
			$("#bg-sec-2-people").stop().animate({"right": -451 + "px"},{duration: 500,easing: "easeInBack"});
			$("#bg-sec-2-tree").stop().animate({"left": -489 + "px"},{duration: 500,easing: "easeInBack"});
			$("#people-2").stop().animate({"right": -320 + "px"},{duration: 500,easing: "easeInBack"});
			$("#bg-dinh").stop().animate({"right": -381 + "px"},{duration: 500,easing: "easeInBack"});
			$("#bg-church").stop().animate({"left": -315 + "px"},{duration: 500,easing: "easeInBack"});
			$("#bg-market").stop().animate({"left": -275 + "px"},{duration: 500,easing: "easeInBack"});
			$("#bg-house").stop().animate({"right": -1040 + "px"},{duration: 500,easing: "easeInBack"});
		}
		if(cgv > 1000) $("#section-3 .content").stop().animate({"opacity": (cgv-1000)/100},{duration: "slow",easing: "easeInOutCubic"});
		
		var height = $(document).height();
		if(cgv > (height-1000)){
			$("#tcb-tower").stop().animate({"left": $(".header").offset().left + 50 + "px"},{duration: 1500,easing: "easeOutBack"});
			$("#people-3").stop().animate({"right": $(".header").offset().left + 110 + "px"},{duration: 1750,easing: "easeOutBack"});
			$("#case-3").stop().animate({"right": $(".header").offset().left + "px"},{duration: 1750,easing: "easeOutBack"});
			$("#sec-3-tree").stop().animate({"opacity": 1},{duration: 1000});
			$("#sec-3-people").stop().animate({"opacity": 1},{duration: 500});
			$("#sec-3-front-house").stop().animate({"opacity": 1},{duration: 2500});
			$("#sec-3-back-house").stop().animate({"opacity": 1},{duration: 3500});
			$("#scroll-now").css("opacity",0);
		}else{
			$("#tcb-tower").stop().animate({"left": -342 + "px"},{duration: 500,easing: "easeInBack"});
			$("#people-3").stop().animate({"right": -320 + "px"},{duration: 500,easing: "easeInBack"});
			$("#case-3").stop().animate({"right": -320 + "px"},{duration: 500,easing: "easeInBack"});
			$("#sec-3-tree").stop().animate({"opacity": 0},{duration: 500});
			$("#sec-3-people").stop().animate({"opacity": 0},{duration: 500});
			$("#sec-3-front-house").stop().animate({"opacity": 0},{duration: 500});
			$("#sec-3-back-house").stop().animate({"opacity": 0},{duration: 500});
			$("#scroll-now").css("opacity",0.5);
		}
	});
	
	// 2.5D
	$(document).mousemove(function(e){
		var x=Math.round(e.pageX-$(document).width()/2);
		$("#bg-case").css({marginRight:x/15});
		$("#people-2").css({marginRight:x/15});
		$("#bg-sec-2-people").css({marginRight:x/35});
		$("#bg-sec-2-tree").css({marginLeft:-1*x/35});
		$("#bg-dinh").css({marginRight: x/55});
		$("#bg-church").css({marginLeft:-1*x/55});
		$("#bg-market").css({marginLeft:-1*x/55});
		$("#bg-house").css({marginRight:x/75});
		$("#case-3").css({marginRight:x/15});
		$("#people-3").css({marginRight:x/15});
		$("#sec-3-tree").css({backgroundPosition: -1*x/30+"px " + "0"});
		$("#sec-3-people").css({backgroundPosition: -1*x/60+"px " + "0"});
		$("#sec-3-front-house").css({backgroundPosition: -1*x/100+"px " + "0"});
		$("#tcb-tower").css({marginLeft:-1*x/120});
		$("#sec-3-back-house").css({backgroundPosition: -1*x/200+"px " + "0"});
	});
	
	// scroll now
	function bounceUp(){
		$("#scroll-now").stop().animate({marginBottom:10},{duration:500,complete:bounceDown});	
	}
	
	function bounceDown(){
		$("#scroll-now").stop().animate({marginBottom:0},{duration:500,complete:bounceUp});
	}
	
	// spritely
	$("#logo-ct").sprite({fps: 10, no_of_frames: 10}).active();
	$("#people-2").sprite({fps: 10, no_of_frames: 10}).active();
	$("#people-3").sprite({fps: 10, no_of_frames: 10}).active();
	$("#sec-3-santa").sprite({fps: 10, no_of_frames: 10}).active();
	$("#santa-w-card").sprite({fps: 20, no_of_frames: 10}).active();
	$("#reindeer").sprite({fps: 20, no_of_frames: 10}).active();
	$("#bottom-header").sprite({fps: 5, no_of_frames: 5}).active();
	$("#snow-tail").sprite({fps: 10, no_of_frames: 10}).active();
	
	
	// init
	$(document).snowfall({flakeCount : 150, maxSpeed : 5, maxSize : 5, round: true});
	$("#section-1 .content").stop().animate({opacity:1},{duration:3000});
	sleigh.stop().animate({left:$(".header").offset().left + "px"},{duration: 1500,easing: "easeInOutCubic"});
	var newYear = new Date(); 
	newYear = new Date(2012, 11-1, 21,12,00); 
	$('#countdown-1').countdown({until: newYear});
	$('#countdown-2').countdown({until: newYear});
	bounceUp();
	
	// jQuery Validation
	$('#customer_form').validate({
		rules: {
			customer_name: {
				minlength: 4,
				required: true
			},
			customer_phone: {
				minlength: 10,
				required: true
			},
			customer_email: {
				email: true,
				required: true
			},
			customer_address: {
				required: true
			},
			customer_card: {
				required: true
			},
			customer_income: {
				required: true
			},
			customer_career: {
				required: true
			},
			customer_passport: {
				minlength: 5,
				required: true
			}
		},
		invalidHandler: function(e, validator) {
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'Chỉ còn 1 thông tin chưa điền, xin hãy hoàn thành nốt.'
					: 'Bạn thiếu ' + errors + ' thông tin.  Xin hãy hoàn thành đầy đủ.';
				$("div.error span").html(message);
				$("div.error").show();
			} else {
				$("div.error").hide();
			}
		},
		highlight: function(label) {
			$(label).closest('p').addClass('error');
		}
	});
	
	// Verify reCaptcha
	$(".verify-close").click(function() {
		$(".verify-alert").fadeOut('fast');
	});	
	
	// jQuery Photo list
	$('.photo-list-thumb').mouseover(function() {
		var title = $(this).attr("title");
		var time = $(this).attr("time");
		var author = $(this).attr("author");
		var votes = $(this).attr("votes");
		var comment = $(this).attr("comment");
		$(".photo-list-title").text(title);
		$(".photo-list-time").text(time);
		$(".photo-list-author").text(author);
		$(".photo-list-votes").text(votes);
		$(".photo-list-comment").text(comment);
	});

	$('.photo-list-thumb').mouseout(function() {
		$(".photo-list-title").text('');
		$(".photo-list-time").text('');
		$(".photo-list-author").text('');
		$(".photo-list-votes").text('');
		$(".photo-list-comment").text('');
	});

	$('.photo-list-main').mouseover(function(e) {
		var t = Math.round(e.pageX - $(window).width() / 2);
		$(this).stop().animate({ left: -1 * t / 15 + "px" });
	});	
	$('.photo-list-main').mouseout(function() {
		$(this).stop().animate({ left: 0 + "px" });
	});
	
	// Scrollbar in Single page
	var photoSingle = $('.photo-single-right').height();
	var sitePlugin = $('.site-plugin').height();
	var socialPlugin = photoSingle - sitePlugin;
	$('.social-plugin').css('height', socialPlugin);
	
});